<html>
<head><title>CS143 Project 1B Query</title></head>
<body>
<p> Type an SQL query in the following box:
<p>
<form action="query.php" method="GET">
<textarea name="query" cols="60" rows="8"></textarea>
<input type="submit" value="Submit" />
</form>
</p>
<p><small>Note: tables and fields are case sensitive. Run "show tables" to see the list of
available tables.</small>
</p>


</body>
</html>

<?php

	$query = $_GET['query'];
	if($query == NULL)
		exit();

	$conn = mysql_connect("localhost", "cs143", "");
	
	if(!$conn) {
		$errmsg = mysql_error($conn);
		print "Connection failed: $errmsg <br />:";
		exit(1);
	}
	
	mysql_select_db("CS143", $conn);

	$query = mysql_real_escape_string($query, $conn);
	$query = str_replace("\\r", "", $query);
	$query = str_replace("\\n", " ", $query);
	$query = str_replace("\\'", "'", $query);
	
	$result = mysql_query($query, $conn);
	if(mysql_errno($conn) != "")
	{
		echo "<strong>ERROR:</strong>\n" . mysql_errno($conn) . ": " . mysql_error($conn) . "\n";
		exit();
	}
	
	echo("<table>");
	while ($row = mysql_fetch_row($result))
	{
		echo("<tr>");
		foreach($row as $col)
		{
			echo("<td>$col</td>");
		}
		echo("</tr>");
	}
	echo("</table>");
	mysql_close($conn);
?>